define(function(require, exports, module){
	require("resource/js/easyui/themes/icon.css");
	require("oa/approval/css/approval.css");
	require("oa/act/approval/form/css/form.css");
	require("resource/js/easyui/easyloader.js");
	var T = require("resource/js/component/treeutil.js");
	var ajax = require("resource/js/component/ajax.js");
	require("resource/js/component/rules.js");
	var EUtil = require("resource/js/component/easyutil.js");
	var ECombobox = require("resource/js/component/easycombobox.js");
	ECombobox.create({
		url:BASEPATH+"/base/authoriza/basecode/getType.htm",
		param:{checkeds:"processStatus"}
	});
	var pageNo = 1,pageSize = 8;
	function initPage(){
		approvalQuery(true,1,8);
		initSearchBox();
		$("#searchform").show();
		
	}
	var initSearchBox = function(){
		using(["combobox"],function(){
			ECombobox.init({ ele:"#status", grouptype:"processStatus",onChange:function(newValue,oldValue){
				var processkey = $('#processkey').combobox('getValue');
				var starttime = $('#starttime').datebox('getValue');
				var endtime = $('#endtime').datebox('getValue');
				var checkeds = $("#checkeds").val();
				var userName = $('#usernames').textbox('getValue');
				approvalQuery(true,1,8,newValue,processkey,starttime,endtime,checkeds,userName);
			}});
			ajax.init().success(function(code,msg,data){
				if(code==0){
					var arrasys = [ {
						id : '',
						name : '请选择'
					} ];
					var retData = arrasys.concat(data);
					$('#processkey').combobox({
					valueField : 'id',
					textField : 'name',
					data : retData,
					onChange : function(newValue, oldValue) {
						var status = $('#status').combobox('getValue');
						var starttime = $('#starttime').datebox('getValue');
						var endtime = $('#endtime').datebox('getValue');
						var checkeds = $("#checkeds").val();
						var userName = $('#usernames').textbox('getValue');
						approvalQuery(true,1,8,status,newValue,starttime,endtime,checkeds,userName);
					}
				});
				}

			}).post(BASEPATH+"/oa/act/generalOperate/getProcessList.htm",{"moudelType":"power_type_3"})
		});
		initUserTree();
	};
	var selUserIds='',selUserNames='';
	var initUserTree = function(){
		$("#usernames").textbox({
			//editable:false,
			prompt: '请选择申请人',
			buttonText:'请选择',    
		    iconCls:'icon-man', 
		    iconAlign:'left', 
		    onClickButton:function(){
				using(["dialog"],function(){
					selUserIds = "";
					selUserNames = "";
					$("#treedld").dialog({
						iconCls: 'icon-save',
					    buttons:[{}]
					});

					var check = $("#usernames").textbox('getValue');
					var tree = new T.ajaxTree({
						eid:"treedld",
						url:BASEPATH+"/base/authoriza/user/getUserListByDeptId.htm",
						check:"checkbox",
						title:"申请人",
						data: {
							simpleData: {
								enable: true,
								idKey: "id",
								pIdKey: "pid",
								rootPId: 0
							}
						},
						save: function(data,args,treeObj){
							checkUserIds = "";
							checkUserNames = "";
							if(data){
								$.each(data,function(i,item){
									if(item.type=='USER'){
										checkUserIds+=item.id+',';
										checkUserNames+=item.name+',';
									}
								});
								checkUserIds = checkUserIds.substring(0, checkUserIds.length-1);
								checkUserNames = checkUserNames.substring(0, checkUserNames.length-1);
								var allUser =  treeObj.transformToArray(treeObj.getNodes());
								if(data.length == 0){
									checkUserNames = '全体成员';
								}
							}

							$("#usernames").textbox('setValue',checkUserNames);
							$("#checkeds").val(checkUserIds);
							var status = $('#status').combobox('getValue');
							var processkey = $('#processkey').combobox('getValue');
							var starttime = $('#starttime').datebox('getValue');
							var endtime = $('#endtime').datebox('getValue');
							approvalQuery(true,1,8,status,processkey,starttime,endtime,checkUserIds);
							tree.close();
						},
					});
					var callback = function(){
						check&&check.length>0
						&&$('.dialog-toolbar').find('.textbox-f').textbox("setValue",check)
						&&$('.dialog-toolbar').find('.textbox-button').click();
					}
					tree.load(callback);			
					tree.open();
				});
			
		    }
		});
	};
	
	
	var openDetails = function(row) {
		$('#remindersFile').hide();
		 $(".jgui-drawer-right").removeClass("jgui-drawer-close").addClass("jgui-drawer-open");
		 $(".duty_drawer").show();
		 $("#jgui-title").text('审批查询（'+row.processname+'）');
		var urlPath = "";
//		if ((row.status == '0' || row.status == '2')&&row.datastatus == '0') {
			 urlPath = BASEPATH
					+ "/oa/act/activitiMain/getProcessViewPage.htm?key="
					+ row.processkey + "&bussId="
					+ row["businessid"];	
//		}else {
//			 urlPath = BASEPATH
//					+ "/view/oa/act/main/actProcessHistory?url="
//					+ row["path"]+"&bussId=" + row["businessid"];
//		}
		$("#myiframe").attr("src",urlPath);
		if(row.assignee){
			//催办
			$('#remindersFile').show();
			$("#remindersFile").unbind("click")
		    $("#remindersFile").on("click",function(){
		    	ajax.init().success(function(code,msg,data){
					if(code==0){
						$(".jgui-drawer-right").find("span.close").click();
						approvalQuery(true,1,8);
					}
					$.messager.show({ title:'系统提示', msg:msg, timeout:5000, showType:'slide' });
				}).post(BASEPATH + "/oa/act/actExApproveInfo/doSendMsg.htm",{"processkey" : row.processkey,"checkeds" : row.assignee})
		    });
		}
	  }
	
	//审批查询
	var colorNumber = {
			0 : "orange",
			1 : "green",
			2 : "purple",
			3 : "red",
			4 : "grey"
	}
	var dataStatus = {
			0: "正常",
			1: "删除",
			2: "归档",
	};
	function parseAbstract(abstractStr){
		var separator = "@VITO@";
		if(abstractStr){
			return abstractStr.split(separator);
		}
	}
	var approvalQuery = function(flag,pageNo,pageSize,status,processkey,starttime,endtime,checkeds,userName){
		if(flag){
			$("#dataList ul").text("");
			$("#dataList").find(".query-more").remove();
		}
		var option = {
				"pageNo":pageNo,
				"pageSize":pageSize,
				"status":status||"",
				"processkey":processkey||"",
				"starttime":starttime||"",
				"endtime":endtime||"",
				"checkeds":checkeds || "",
				"username":userName
		    }
		ajax.init().success(function(code,msg,data){
			if(code==0){
				if(data.rows.length>0){
					$("#noData").hide();
					for(var k in data.rows){
						var abstractStr = data.rows[k]['abstract'] || '';
						if(!data.rows[k].processname){
							data.rows[k].processname="流程已被删除"
						}
						$("<li><div><span class="+colorNumber[data.rows[k].status]+">"+data.rows[k].statustext+"</span></div>" +
								"<div class='conameSpan' style='width:20%'><span>"+workBeanchTrim(data.rows[k].processname)+"</span><span>"+contentNotempty(parseAbstract(abstractStr)&&parseAbstract(abstractStr)[0])+"</span></div>" +
								"<div style='width:15%'>发起人："+workBeanchTrim(data.rows[k].username)+"</div>" +
								"<div >"+showEmptyEnd(data.rows[k].assigneename,'已完结')+"</div>" +
								"<div >发起时间："+data.rows[k].starttime+"</div>"+
								"<div >结束时间："+(data.rows[k].endtime||'')+"</div></li>")
								.appendTo($("#dataList ul")).each(function(){
									$.data(this,{data:data.rows[k]})
								}).on("click",function(){
									$(this).addClass("selected").siblings().removeClass("selected");
						    	    openDetails($.data(this).data);
						      });;
					}
					$("#dataList").find(".query-more").remove();
					if(data.rows&&data.rows.length == pageSize && data.total>8){
						$("<div class='query-more'><a>查看更多</a></div>").appendTo($("#dataList")).on("click",function(){
							pageNo++;
							approvalQuery(false,pageNo,pageSize,status,processkey,starttime,endtime,checkeds);
						});
					}else{
						if(data.total>8){
							$("<div class='query-more'><a style='cursor: default;'>没有更多数据了！</a></div>").appendTo($("#dataList"));
						}
					}
				}else{
					$("#dataList").find(".query-more").remove();
					if(data.total == 0){
						$("#noData").show();
					}else{
						$("<div class='query-more'><a style='cursor: default;'>没有更多数据了！</a></div>").appendTo($("#dataList"));
					}
				}
				$(".jgui-drawer-right").find(".close").click(function(){
					$(".jgui-drawer-right").addClass("jgui-drawer-close").removeClass("jgui-drawer-open");
				});
			}
		}).post(BASEPATH + "/oa/act/actExApproveInfo/queryLaunchByRootDept.htm",option);
	}
	
	
	$("#findQuery").bind("click",function(){
		var status = $('#status').combobox('getValue');
		var processkey = $('#processkey').combobox('getValue');
		var starttime = $('#starttime').datebox('getValue');
		var endtime = $('#endtime').datebox('getValue');
		var checkeds = $("#checkeds").val();
		var userName = $('#usernames').textbox('getValue');
		approvalQuery(true,1,8,status,processkey,starttime,endtime,checkeds,userName);
	});
	$("#exportExcel").bind("click",function(){
		var status = $('#status').combobox('getValue');
		var processkey = $('#processkey').combobox('getValue');
		var starttime = $('#starttime').datebox('getValue');
		var endtime = $('#endtime').datebox('getValue');
		var checkeds = $("#checkeds").val();
		if(processkey && processkey != ""){
			window.location.href=BASEPATH+"/oa/act/print/exportExcel.htm?status="+status+"&processkey="+processkey+"&starttime="+starttime+"&endtime="+endtime+"&checkeds="+checkeds;
		}else{
			$.messager.alert("系统提示","导出条件需要是同一流程类型");
		}
	})
	
	var workBeanchTrim = function(res){
		return res || "";
	}
	
	//完结和终止的时候，下级审批人显示完结或者终止
	var showEmptyEnd = function(content,status){
		if(content){
			return "下级审批人：" + content;
		}else{
			return status;
		}
	}
	
	//内容不能为空，超过30字符显示......
	var contentNotempty =function(content){
		if(content){
			if(content.toString().length>30){
				return content.toString().substring(0,30)+'......';
			}else if(content.toString().length<=30){
				return content.toString().substring(0,30);
			}
		}else{
			return "";
		}
	}
	
	module.exports = {
		trans:function(id){
				using(["parser","messager","textbox","combobox"],function(){
					setTimeout(initPage(),50);
				});
		}
	}
});